<?php

namespace app\admin\model\material;

use app\common\exception\LogicException;
use app\common\library\Queue;
use app\common\logic\Attach;
use app\common\model\BaseModel;
use eduline\upload\stocks\bokecc\File;
use Exception;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;

/**
 *
 */
class Material extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'material';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 无需创建时间
     *
     * @var boolean
     */
    protected $createTime = false;
    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 定义所有素材类型
     *
     * @var array
     */
    protected $materialTypes = ['picture' => 1, 'audio' => 2, 'video' => 3, 'file' => 4];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 设置素材类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-21
     *
     * @param    [type]                         $type [description]
     */
    public function setMaterialTypeAttr($type)
    {
        return $this->materialTypes[$type];
    }

    /**
     * 素材类型查询条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-21
     *
     * @param    [type]                         $query [description]
     * @param string $type [description]
     */
    public function scopeMaterialType($query, string $type = 'picture')
    {
        $type = $this->materialTypes[$type] ?? 0;
        $query->where('material_type', $type);
    }

    /**
     * 关联附件信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-21
     */
    public function attach(): BelongsTo
    {
        return $this->belongsTo(Attach::class, 'attach_id', 'id')->bind(['filename', 'fileurl', 'filesize', 'duration_format']);
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     */
    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     */
    public static function onBeforeInsert($model)
    {
        // 机构
        $model->mhm_id = $model->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     */
    public static function onAfterInsert($model)
    {
        if ($model->material_category_id != -1 && $model->material_type == 3) {
            // CC云视频分类ID
            $cloudId = app(Category::class)->where('id', $model->material_category_id)->value('cloud_id');
            if ($cloudId) {
                // 附件未上传云端，队列处理
                $attach = app(Attach::class)->findOrEmpty($model->attach_id);
                if ($attach->isEmpty()) exit;
                if ($attach->status == 2) {
                    Queue::push('BokeccUpdate', ['attachId' => $model->attach_id, 'cloudId' => $cloudId], 5);
                    exit;
                }
                // 附件上传云端，修改云端视频分类
                $bokecc = new File();
                try {
                    $re = $bokecc->updateVideo($model->attach_id, $cloudId);
                    if (isset($re['error'])) Queue::push('BokeccUpdate', ['attachId' => $model->attach_id, 'cloudId' => $cloudId], 10);
                } catch (Exception $e) {
                    throw new LogicException($e->getMessage());
                }
            }
        }
    }

}
